package com.example.controller;

import com.example.domain.CultureShoppingCart;
import com.example.service.CultureShoppingCartService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (CultureShoppingCart)表控制层
 *
 * @author makejava
 * @since 2025-03-29 17:58:45
 */
@RestController
@RequestMapping("/front/cultureShoppingCart")
public class CultureShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private CultureShoppingCartService cultureShoppingCartService;

    /**
     * 分页查询
     *
     * @param cultureShoppingCart 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<CultureShoppingCart>> queryByPage(CultureShoppingCart cultureShoppingCart,
                                                                 @RequestParam(defaultValue = "0") int page,
                                                                 @RequestParam(defaultValue = "10") int size,
                                                                 @RequestParam(defaultValue = "name,asc") String sort) {

        String[] sortParams = sort.split(",");
        Sort sortObj = Sort.by(Sort.Direction.fromString(sortParams[1]), sortParams[0]);
        PageRequest pageRequest = PageRequest.of(page, size, sortObj);
        return ResponseEntity.ok(this.cultureShoppingCartService.queryByPage(cultureShoppingCart, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<CultureShoppingCart> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cultureShoppingCartService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param cultureShoppingCart 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<CultureShoppingCart> add(CultureShoppingCart cultureShoppingCart) {
        return ResponseEntity.ok(this.cultureShoppingCartService.insert(cultureShoppingCart));
    }

    /**
     * 编辑数据
     *
     * @param cultureShoppingCart 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<CultureShoppingCart> edit(CultureShoppingCart cultureShoppingCart) {
        return ResponseEntity.ok(this.cultureShoppingCartService.update(cultureShoppingCart));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer productId,Integer userId) {
        return ResponseEntity.ok(this.cultureShoppingCartService.deleteById(productId,userId));
    }

}

